package com.zhiyou100.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.tools.javac.code.Source;
import com.zhiyou100.model.MUser;
import com.zhiyou100.model.ResultObject;
import com.zhiyou100.service.BackUserControllerService;
import com.zhiyou100.util.BackgroundLogAnno;

@Controller
public class BackUserController {
	@Autowired
	private BackUserControllerService buc;
	
	@BackgroundLogAnno("进入后台用户管理")
	@RequestMapping("/background/user/list")
	public String baseUserList(@RequestParam(defaultValue="1")int pageNo,
			@RequestParam(defaultValue="")String mid,
			@RequestParam(defaultValue="")String musername,Model model) {
		Map<String,Object> map = new HashMap<>(); 
		map.put("mid", mid);
		map.put("musername", musername);
		System.out.println("map: "+map);
		PageHelper.startPage(pageNo, 10);
		List<MUser> muser = buc.findAll(map);
		PageInfo<MUser> pageInfo = new PageInfo<>(muser);
		model.addAttribute("map", map);
		model.addAttribute("muser", pageInfo.getList());
		model.addAttribute("page", pageInfo);
		return "forward:/admin/view/muser/muserList.jsp";
		
	}
	
	@RequestMapping(value="/background/user/add",method=RequestMethod.GET)
	public String userAdd() {
		return "forward:/admin/view/muser/muserAdd.jsp";
	}
	
	// 验证码提取
	@BackgroundLogAnno("后台用户添加获取验证码")
	@RequestMapping("/verification")
	@ResponseBody
	public ResultObject getVerif(Model model) {
		System.out.println("随机生成一个六位数");
		 int i = new Random().nextInt(999999);
	        if(i<100000) {
	        	i+=100000;
	        }
	     model.addAttribute("i", i);
	     System.out.println("i: "+i);
		return new ResultObject("200","成功",i);
	     
	}
	// 添加
	@BackgroundLogAnno("后台用户添加")
	@RequestMapping(value="/background/user/add",method=RequestMethod.POST)
	public String userAdd2(MUser muser) {
		System.out.println("添加muser: "+muser);
		buc.userAdd(muser);
		return "forward:/background/user/list";
	}
	
	// 删除
	@BackgroundLogAnno("后台用户删除")
	@RequestMapping("/background/delete")
	public String userDelete(@RequestParam(defaultValue="")String id) {
		System.out.println("删除用户id: "+id);
		buc.userDelete(id);
		return "forward:/background/user/list";
	}
	// 编辑
	@BackgroundLogAnno("后台用户修改的查找")
	@RequestMapping(value="/background/user/detail",method=RequestMethod.GET)
	public String userUpdate(@RequestParam(defaultValue="")String id,Model model) {
		System.out.println("更新用户查找的id: "+id);
		MUser muser = buc.userUpdate(id);
		System.out.println("更新用户查找muser: "+muser);
		model.addAttribute("muser", muser);
		return "forward:/admin/view/muser/muserUpdate.jsp";
	}
	@BackgroundLogAnno("后台用户信息的修改")
	@RequestMapping(value="/background/user/detail",method=RequestMethod.POST)
	public String userUpdate2(MUser muser) {
		System.out.println("执行更新得到的muser; "+muser);
		buc.updateUserById(muser);
		return "forward:/background/user/list";
	}
}
